home *** CD-ROM | disk | FTP | other *** search
/ The Original Shareware 1.1 / The Original Shareware (WeMake CDs)(Volume 1.1)(CDs, Inc)(1993).iso / 6 / 3dlib.zip / DEMO3D.C < prev    next >
Text File  |  1988-02-28  |  8KB  |  273 lines

  1. #include <3d.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4. #include <float.h>
  5. #include <graphics.h>
  6.  
  7. void main()
  8.  
  9. /* 3D Transforms Demonstration Program.  Requires Turbo C Version 1.5.
  10.    Project file should contain the line
  11.  
  12.                        DEMO3D.C GRAPHICS.LIB 3D.LIB
  13.  
  14.    The library directory must include 3D.LIB and GRAPHICS.LIB, and the
  15.    include directory must include 3D.H.  The default directory must contain
  16.    the driver for your graphics hardware.
  17.  
  18.    The program displays a cube, a tetrahedron, and an octahedron in
  19.    three dimensions.  Each figure is rotated about a different axis. */
  20.  
  21. {
  22.     FACE *f,*g,*h,*i,*j,*k;                /* Faces of cube */
  23.     FACE *t1,*t2,*t3,*t4;                  /* Faces of tetrahedron */
  24.     FACE *o1,*o2,*o3,*o4,*o5,*o6,*o7,*o8;  /* Faces of octahedron */
  25.     VECTOR n,s;
  26.     MATRIX xm,ym,xr,yr,om,or,id;           /* Transformation matrices */
  27.     OBJECT *o;                             /* Cube */
  28.     OBJECT *p;                             /* Tetrahedron */
  29.     OBJECT *q;                             /* Octahedron */
  30.     int g_driver,g_mode;
  31.     int apage,vpage,tpage,frame;
  32.  
  33.     /* Initialize matrices and data structures */
  34.  
  35.     identity (xm);
  36.     identity (ym);
  37.     identity (xr);
  38.     identity (yr);
  39.     identity (id);
  40.     identity (om);
  41.     identity (or);
  42.  
  43.     f = (FACE *)malloc(sizeof(FACE));
  44.     g = (FACE *)malloc(sizeof(FACE));
  45.     h = (FACE *)malloc(sizeof(FACE));
  46.     i = (FACE *)malloc(sizeof(FACE));
  47.     j = (FACE *)malloc(sizeof(FACE));
  48.     k = (FACE *)malloc(sizeof(FACE));
  49.  
  50.     t1 = (FACE *)malloc(sizeof(FACE));
  51.     t2 = (FACE *)malloc(sizeof(FACE));
  52.     t3 = (FACE *)malloc(sizeof(FACE));
  53.     t4 = (FACE *)malloc(sizeof(FACE));
  54.  
  55.     o1 = (FACE *)malloc(sizeof(FACE));
  56.     o2 = (FACE *)malloc(sizeof(FACE));
  57.     o3 = (FACE *)malloc(sizeof(FACE));
  58.     o4 = (FACE *)malloc(sizeof(FACE));
  59.     o5 = (FACE *)malloc(sizeof(FACE));
  60.     o6 = (FACE *)malloc(sizeof(FACE));
  61.     o7 = (FACE *)malloc(sizeof(FACE));
  62.     o8 = (FACE *)malloc(sizeof(FACE));
  63.  
  64.     o = (OBJECT *)malloc(sizeof(OBJECT));
  65.     new_obj (o);
  66.  
  67.     p = (OBJECT *)malloc(sizeof(OBJECT));
  68.     new_obj (p);
  69.  
  70.     q = (OBJECT *)malloc(sizeof(OBJECT));
  71.     new_obj (q);
  72.  
  73.     /* Define cube */
  74.  
  75.     new_face (f);
  76.     add_corner (   0.0,   0.0,   0.0,f);
  77.     add_corner ( 100.0,   0.0,   0.0,f);
  78.     add_corner ( 100.0, 100.0,   0.0,f);
  79.     add_corner (   0.0, 100.0,   0.0,f);
  80.     add_face (o,f);
  81.  
  82.     new_face (g);
  83.     add_corner (   0.0,   0.0,   0.0,g);
  84.     add_corner (   0.0, 100.0,   0.0,g);
  85.     add_corner (   0.0, 100.0, 100.0,g);
  86.     add_corner (   0.0,   0.0, 100.0,g);
  87.     add_face (o,g);
  88.  
  89.     new_face (h);
  90.     add_corner (   0.0,   0.0,   0.0,h);
  91.     add_corner (   0.0,   0.0, 100.0,h);
  92.     add_corner ( 100.0,   0.0, 100.0,h);
  93.     add_corner ( 100.0,   0.0,   0.0,h);
  94.     add_face (o,h);
  95.  
  96.     new_face (i);
  97.     add_corner (   0.0,   0.0, 100.0,i);
  98.     add_corner (   0.0, 100.0, 100.0,i);
  99.     add_corner ( 100.0, 100.0, 100.0,i);
  100.     add_corner ( 100.0,   0.0, 100.0,i);
  101.     add_face (o,i);
  102.  
  103.     new_face (j);
  104.     add_corner (   0.0, 100.0,   0.0,j);
  105.     add_corner ( 100.0, 100.0,   0.0,j);
  106.     add_corner ( 100.0, 100.0, 100.0,j);
  107.     add_corner (   0.0, 100.0, 100.0,j);
  108.     add_face (o,j);
  109.  
  110.     new_face (k);
  111.     add_corner ( 100.0,   0.0,   0.0,k);
  112.     add_corner ( 100.0,   0.0, 100.0,k);
  113.     add_corner ( 100.0, 100.0, 100.0,k);
  114.     add_corner ( 100.0, 100.0,   0.0,k);
  115.     add_face (o,k);
  116.  
  117.     /* Define tetrahedron */
  118.  
  119.     new_face (t1);
  120.     add_corner (   0.00,   0.00,   0.00,t1);
  121.     add_corner ( 100.00,   0.00,   0.00,t1);
  122.     add_corner (  50.00,  81.65,  28.87,t1);
  123.     add_face (p,t1);
  124.  
  125.     new_face (t2);
  126.     add_corner (   0.00,   0.00,   0.00,t2);
  127.     add_corner (  50.00,   0.00,  86.60,t2);
  128.     add_corner ( 100.00,   0.00,   0.00,t2);
  129.     add_face (p,t2);
  130.  
  131.     new_face (t3);
  132.     add_corner (   0.00,   0.00,   0.00,t3);
  133.     add_corner (  50.00,  81.65,  28.87,t3);
  134.     add_corner (  50.00,   0.00,  86.60,t3);
  135.     add_face (p,t3);
  136.  
  137.     new_face (t4);
  138.     add_corner ( 100.00,   0.00,   0.00,t4);
  139.     add_corner (  50.00,   0.00,  86.60,t4);
  140.     add_corner (  50.00,  81.65,  28.87,t4);
  141.     add_face (p,t4);
  142.  
  143.     /* Define octahedron */
  144.  
  145.     new_face (o1);
  146.     add_corner (   0.00,  70.71,   0.00,o1);
  147.     add_corner (  50.00,   0.00,  50.00,o1);
  148.     add_corner ( 100.00,  70.71,   0.00,o1);
  149.     add_face (q,o1);
  150.  
  151.     new_face (o2);
  152.     add_corner ( 100.00,  70.71,   0.00,o2);
  153.     add_corner (  50.00,   0.00,  50.00,o2);
  154.     add_corner ( 100.00,  70.71, 100.00,o2);
  155.     add_face (q,o2);
  156.  
  157.     new_face (o3);
  158.     add_corner ( 100.00,  70.71, 100.00,o3);
  159.     add_corner (  50.00,   0.00,  50.00,o3);
  160.     add_corner (   0.00,  70.71, 100.00,o3);
  161.     add_face (q,o3);
  162.  
  163.     new_face (o4);
  164.     add_corner (   0.00,  70.71, 100.00,o4);
  165.     add_corner (  50.00,   0.00,  50.00,o4);
  166.     add_corner (   0.00,  70.71,   0.00,o4);
  167.     add_face (q,o4);
  168.  
  169.     new_face (o5);
  170.     add_corner (   0.00,  70.71,   0.00,o5);
  171.     add_corner (  50.00, 141.40,  50.00,o5);
  172.     add_corner (   0.00,  70.71, 100.00,o5);
  173.     add_face (q,o5);
  174.  
  175.     new_face (o6);
  176.     add_corner (   0.00,  70.71, 100.00,o6);
  177.     add_corner (  50.00, 141.40,  50.00,o6);
  178.     add_corner ( 100.00,  70.71, 100.00,o6);
  179.     add_face (q,o6);
  180.  
  181.     new_face (o7);
  182.     add_corner ( 100.00,  70.71, 100.00,o7);
  183.     add_corner (  50.00, 141.40,  50.00,o7);
  184.     add_corner ( 100.00,  70.71,   0.00,o7);
  185.     add_face (q,o7);
  186.  
  187.     new_face (o8);
  188.     add_corner ( 100.00,  70.71,   0.00,o8);
  189.     add_corner (  50.00, 141.40,  50.00,o8);
  190.     add_corner (   0.00,  70.71,   0.00,o8);
  191.     add_face (q,o8);
  192.  
  193.     /* The light source in the z direction (from the eye) */
  194.  
  195.     s[0] =  0.25;
  196.     s[1] =  0.0;
  197.     s[2] =  1.0;
  198.  
  199.     /* Center figures in space and orient them */
  200.  
  201.     /* The following series of function calls illustrates the concatenation
  202.        of 3D transforms.  Each of three matrices xm, ym, and om is the
  203.        concatenation of four transforms; first, center the object on the
  204.        origin, second, rotate the object about the y axis, third, rotate the
  205.        object about the x axis, and fourth, translate the object to its final
  206.        position.  All four transforms are concatenated in each matrix, then
  207.        the object is transformed.  Thus, each vertex in the object is mul-
  208.        tiplied by the transformation matrix only once.    */
  209.  
  210.     trans (-50,-50,-50,xm);
  211.     trans (-50,-50,-50,ym);
  212.     trans (-50,-50,-50,om);
  213.     yrot (M_PI/4,xm);
  214.     yrot (M_PI/3,ym);
  215.     yrot (M_PI/4,om);
  216.     xrot (M_PI/4,xm);
  217.     xrot (M_PI/3,ym);
  218.     xrot (M_PI/4,om);
  219.  
  220.     /* Position figures in space */
  221.  
  222.     trans (200,200,200,xm);
  223.     trans (400,150,200,ym);
  224.     trans (500,250,200,om);
  225.     xform (*o,xm);
  226.  
  227.     xform (*p,ym);
  228.     xform (*q,om);
  229.  
  230.     /* Set up rotation matrices */
  231.  
  232.     /* These matrices are created to perform an incremental rotation of each
  233.        object.  First, the object is translated to the origin, second, the
  234.        object is rotated, and third, the object is translated back to its
  235.        original postion. */
  236.  
  237.     trans (-200,-200,-200,xr);
  238.     yrot (M_PI/45,xr);
  239.     trans (200,200,200,xr);
  240.  
  241.     trans (-400,-150,-200,yr);
  242.     xrot (M_PI/45,yr);
  243.     trans (400,150,200,yr);
  244.  
  245.     trans (-500,-250,-200,or);
  246.     xrot (M_PI/45,or);
  247.     yrot (M_PI/45,or);
  248.     trans (500,250,200,or);
  249.  
  250.     apage = 0;
  251.     vpage = 1;
  252.  
  253.     registerbgidriver(CGA_driver);
  254.     registerbgidriver(EGAVGA_driver);
  255.     detectgraph (&g_driver,&g_mode);
  256.     initgraph (&g_driver,&g_mode,"");
  257.  
  258.     for (frame = 1; frame <= 45; frame++)
  259.     {
  260.         setvisualpage(vpage);
  261.         setactivepage(apage);
  262.         tpage = apage;
  263.         apage = vpage;
  264.         vpage = tpage;
  265.         cleardevice();
  266.         xform (*o,xr);
  267.         xform (*p,yr);
  268.         xform (*q,or);
  269.         disp_object (s,1,o,id);
  270.         disp_object (s,4,p,id);
  271.         disp_object (s,2,q,id);
  272.     }
  273. }